import MusicManager from "../../MusicManager";
import DestructibleFacility from "../facility/DestructibleFacility";
import NodePoolManager from "../manager/NodePoolManager";

const { ccclass, property } = cc._decorator;

/**
 * 维修 
 */
@ccclass
export default class FacilityRepair extends cc.Component {

    fac: DestructibleFacility = null;

    anim: cc.Animation;

    init(fac: DestructibleFacility): void {
        this.fac = fac;
        this.anim = this.getComponent(cc.Animation);
        this.node.children[0].active = false;
        this.fac.node.on(DestructibleFacility.EventType.摧毁, this.die, this);
    }

    protected update(dt: number): void {
        let isplayering = this.anim.getAnimationState("维修").isPlaying;
        if (this.fac.currentHp < this.fac.maxHp) {
            if (!isplayering) {
                this.node.children[0].active = true;
                MusicManager.instance.play(MusicManager.instance.repair, this.node.children[0], true);
                this.anim.play();
            }
        } else {
            if (isplayering) {
                this.anim.stop();
                this.node.children[0].active = false;
            }
        }
    }

    repair() {
        let one = Math.ceil(this.fac.maxHp / 100);
        this.fac.repairHp(one);
    }

    die() {
        this.anim.stop();
        this.anim = null;
        this.fac.node.targetOff(this); 
        this.fac = null;
        NodePoolManager.instance.recycle(this.node);
    }

}